/*
 * Copyright (c) 2015 [1076559197@qq.com | tchen0707@gmail.com]
 *
 * Licensed under the Apache License, Version 2.0 (the "License”);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android07.newtop.activity;


import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.jude.beam.expansion.BeamBaseActivity;

import android07.newtop.R;
import android07.newtop.view.BrowserLayout;


/**
 * Author:  Tau.Chen
 * Email:   1076559197@qq.com | tauchen1990@gmail.com
 * Date:    15/7/24
 * Description:
 */
public class BaseWebActivity extends BeamBaseActivity {

    public static final String BUNDLE_KEY_URL = "BUNDLE_KEY_URL";
    public static final String BUNDLE_KEY_TITLE = "BUNDLE_KEY_TITLE";
    public static final String BUNDLE_KEY_SHOW_BOTTOM_BAR = "BUNDLE_KEY_SHOW_BOTTOM_BAR";

    private String mWebUrl = null;
    private String mWebTitle = null;
    private boolean isShowBottomBar = true;

    private Toolbar mToolBar = null;
    private BrowserLayout mBrowserLayout = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_common_web);

        initViews();

    }





    protected void initViews() {
        Bundle extras = getIntent().getExtras();
        mWebTitle = extras.getString(BUNDLE_KEY_TITLE);
        mWebUrl = extras.getString(BUNDLE_KEY_URL);
        isShowBottomBar = extras.getBoolean(BUNDLE_KEY_SHOW_BOTTOM_BAR);

//        setSystemBarTintDrawable(getResources().getDrawable(R.drawable.snackbar_shape_book));

        mToolBar = (Toolbar) $( R.id.toolbar);
        mBrowserLayout = (BrowserLayout) $( R.id.common_web_browser_layout);

        if (null != mToolBar) {
            setSupportActionBar(mToolBar);
            getSupportActionBar().setHomeButtonEnabled(true);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }

        if (!TextUtils.isEmpty(mWebTitle)) {
//            setTitle(mWebTitle);
            if (null != mToolBar) {
                mToolBar.setTitle(mWebTitle);
            }
        } else {
            setTitle("网页");
        }

        if (!TextUtils.isEmpty(mWebUrl)) {

            mBrowserLayout.loadUrl(mWebUrl);

        } else {
           // CoCoinUtil.showToast(this,"获取URL地址失败");
            Toast.makeText(this,"获取URL地址失败",Toast.LENGTH_LONG).show();
        }

        if (!isShowBottomBar) {
            mBrowserLayout.hideBrowserController();
        } else {
            mBrowserLayout.showBrowserController();
        }
    }
    /**
     *   use SytemBarTintManager
     *   Android沉浸式状态栏SystemBarTint的使用方法
     *   http://blog.csdn.net/hwe_xc/article/details/50553758
     *   @param tintDrawable  自定义传入的图片
     */
//    protected void setSystemBarTintDrawable(Drawable tintDrawable) {
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            setTranslucentStatus(true);
//            SystemBarTintManager mTintManager = new SystemBarTintManager(this);
//            if (tintDrawable != null) {
//                mTintManager.setStatusBarTintEnabled(true);
//                mTintManager.setNavigationBarTintEnabled(true);
//                mTintManager.setTintDrawable(tintDrawable);   // 自定义图片
//            } else {
//                mTintManager.setStatusBarTintEnabled(false);
//                mTintManager.setTintDrawable(null);
//            }
//        }
//    }

    /**
     *   Android沉浸式状态栏SystemBarTint的使用方法
     *   http://blog.csdn.net/hwe_xc/article/details/50553758
     *   @param color  自定义传入的颜色
     */
//    public void setSystemBarTintDrawable(String color) {
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            setTranslucentStatus(true);
//            SystemBarTintManager mTintManager = new SystemBarTintManager(this);
//            mTintManager.setStatusBarTintEnabled(true);
//            mTintManager.setNavigationBarTintEnabled(true);
//            mTintManager.setTintColor(Color.parseColor(color));  // 自定义颜色
//        }
//    }

    /**ndroid沉浸式状态栏SystemBarTint的使用方法*/
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }
}
